﻿using System;
using Machine.Entity;
using Machine.IRepository;
using System.Collections.Generic;
using SqlSugar;

namespace Machine.Repository
{
    public class MachineWXDAL : BaseDAL<MachineBX>, IMachineWX
    {
        public object GetPageList(string role, string uid, string status, int pageIndex, int pageSize, string recid = "0")
        {
            int totalCount = 0;
            var query = db.Queryable<MachineBX, Machine.Entity.Machine, sys_user, sys_user>(
                (wx, ma, recu, appu) => new object[] {
                    JoinType.Inner, wx.Macid == ma.MacID
                    ,JoinType.Left, wx.Receiver == recu.Employee
                    ,JoinType.Left, wx.Appliant == appu.Employee })
                .Where((wx) => wx.Status == status && wx.Del == false && (recid == "0" || wx.Receiver == recid));
            if (role == "1")
            {
                query = query.Where(wx => wx.Appliant == uid);
            }
            if(status == "20"){
                query = query.OrderBy((wx) => wx.RecTime, OrderByType.Desc);
            }else{
                query = query.OrderBy((wx) => wx.CreateTime, OrderByType.Desc);
            }
            var list = query
                .Select((wx, ma, recu, appu) => new
                {
                    WXID = wx.WXID
                                        ,
                    MacName = ma.MacName
                                        ,
                    MacID = ma.MacID
                                        ,
                    ModelNo = ma.ModelNo
                                        ,
                    kind = wx.kind
                                        ,
                    AppName = appu.UserName
                    ,
                    AppTime = wx.AppTime
                    ,
                    status = wx.Status
                    ,
                    RecName = recu.UserName
                    ,
                    RecTime = wx.RecTime
                    ,
                    MtncContent = wx.MtncContent
                })
                .ToPageList(pageIndex, pageSize, ref totalCount);

            var retObj = new
            {
                recode = 1,
                msg = "成功",
                count = totalCount,
                data = list
            };
            return retObj;
        }

        public object getMachineWX(string WXID)
        {
            var data = db.Queryable<MachineBX, Machine.Entity.Machine, sys_user, sys_user>(
                (wx, ma, recu, appu) => new object[] {
                    JoinType.Inner, wx.Macid == ma.MacID
                    ,JoinType.Left, wx.Receiver == recu.Employee
                    ,JoinType.Left, wx.Appliant == appu.Employee
                 })
                .Where((wx) => wx.WXID == WXID)
                .Select((wx, ma, recu, appu) => new
                {
                    WXID = wx.WXID
                                        ,
                    MacName = ma.MacName
                                        ,
                    MacID = ma.MacID
                                        ,
                    ModelNo = ma.ModelNo
                                        ,
                    kind = wx.kind
                                        ,
                    AppTime = wx.AppTime
                    ,
                    AppName = appu.UserName
                    ,
                    AppPhone = appu.Phone
                    ,
                    RecTime = wx.RecTime
                    ,
                    RecName = recu.UserName
                    ,
                    RecPhone = recu.Phone
                    ,
                    MtncContent = wx.MtncContent
                    ,
                    Status = wx.Status
                    ,
                    Img = wx.Img

                })
                .First();

            var retObj = new
            {
                recode = 1,
                data = data
            };
            return retObj;
        }

        public object getListByKey(string key, string recid = "0", int pageIndex = 1, int pageSize = 10)
        {
            int totalCount = 0;
            var list = db.Queryable<MachineBX, Machine.Entity.Machine, sys_user, sys_user>(
                (wx, ma, recu, appu) => new object[] {
                    JoinType.Inner, wx.Macid == ma.MacID
                    ,JoinType.Left, wx.Receiver == recu.Employee
                    ,JoinType.Left, wx.Appliant == appu.Employee })
                .Where((wx, ma) => wx.Status == "30" && wx.Del == false
                                && (wx.Macid.Contains(key) || wx.MtncContent.Contains(key)) 
                                && (recid == "0" || wx.Receiver == recid))
                .OrderBy((wx, ma) => wx.CreateTime, OrderByType.Desc)
                .Select((wx, ma, recu, appu) => new
                {
                    WXID = wx.WXID
                                        ,
                    MacName = ma.MacName
                                        ,
                    MacID = ma.MacID
                                        ,
                    ModelNo = ma.ModelNo
                                        ,
                    kind = wx.kind
                                        ,
                    AppTime = wx.AppTime
                    ,
                    AppName = appu.UserName
                    ,
                    RecName = recu.UserName
                    ,
                    RecTime = wx.RecTime
                    ,
                    Status = wx.Status
                    ,
                    MtncContent = wx.MtncContent
                })
                .ToPageList(pageIndex, pageSize, ref totalCount);

            var retObj = new
            {
                recode = 1,
                msg = "成功",
                count = totalCount,
                data = list
            };
            return retObj;
        }
        public object getDelList(int pageIndex = 1, int pageSize = 10)
        {
            int totalCount = 0;
            var list = db.Queryable<MachineBX, Machine.Entity.Machine, sys_user, sys_user>(
                (wx, ma, recu, appu) => new object[] {
                    JoinType.Inner, wx.Macid == ma.MacID
                    ,JoinType.Left, wx.Receiver == recu.Employee
                    ,JoinType.Left, wx.Appliant == appu.Employee })
                .Where((wx) => wx.Status != "40" && wx.Del == true)
                .OrderBy((wx) => wx.CreateTime, OrderByType.Desc)
                .Select((wx, ma, recu, appu) => new
                {
                    WXID = wx.WXID
                                        ,
                    MacName = ma.MacName
                                        ,
                    MacID = ma.MacID
                                        ,
                    ModelNo = ma.ModelNo
                                        ,
                    kind = wx.kind
                                        ,
                    AppTime = wx.AppTime
                    ,
                    AppName = appu.UserName
                    ,
                    RecName = recu.UserName
                    ,
                    RecTime = wx.RecTime
                    ,
                    Status = wx.Status
                    ,
                    MtncContent = wx.MtncContent
                })
                .ToPageList(pageIndex, pageSize, ref totalCount);

            var retObj = new
            {
                recode = 1,
                msg = "成功",
                count = totalCount,
                data = list
            };
            return retObj;
        }
        public int updateEntity(MachineBX Entity)
        {
            return db.Updateable(Entity).ExecuteCommand();
        }

        public int updateWX(string wxid, string uid)
        {
            return db.Updateable<MachineBX>()
            .UpdateColumns(it => new MachineBX() { Status = "UN", Receiver = uid })
            .Where(it => it.WXID == wxid)
            .ExecuteCommand();
        }

        public int updateFN(string wxid, string uid)
        {
            return db.Updateable<MachineBX>()
            .UpdateColumns(it => new MachineBX() { Status = "FN" })
            .Where(it => it.WXID == wxid)
            .ExecuteCommand();
        }

        public int updateCN(string wxid)
        {
            return db.Updateable<MachineBX>()
            .UpdateColumns(it => new MachineBX() { Status = "CN" })
            .Where(it => it.WXID == wxid)
            .ExecuteCommand();
        }
    }
}
